import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {
    // 创建对象
    public static void reflectNewInstance()  {
        Class<?> stu1;

        try {
            stu1 = Class.forName("Student"); //拿到Class<Student>类，只有1个
            Student student=(Student)stu1.newInstance(); //生成实例，可以有多个
            System.out.println(student);
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    // 反射私有的构造方法  屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor(){
        Class<?> stu1;

        try {
            stu1 = Class.forName("Student"); //拿到Class<Student>类，只有1个
            //拿到指定类型的构造器
            Constructor<?> constructor=stu1.getDeclaredConstructor(String.class,int.class);

            constructor.setAccessible(true);
            //用该构造器生成实例
            Student student=(Student)constructor.newInstance("张三",18);
            System.out.println(student);

        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有属性
    public static void reflectPrivateField(){
        Class<?> stu1;

        try {
            stu1 = Class.forName("Student"); //拿到Class<Student>类，只有1个
            Field field=stu1.getDeclaredField("name");
            field.setAccessible(true);
            Student student=(Student)stu1.newInstance();
            field.set(student,"李四");
            System.out.println(student);
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有方法
    public static void reflectPrivateMethod(){
        Class<?> stu1;

        try {
            stu1 = Class.forName("Student"); //拿到Class<Student>类，只有1个
            Student student=(Student)stu1.newInstance(); //生成实例，可以有多个

            //拿到指定方法构造器
            Method method=stu1.getDeclaredMethod("function", String.class);
            //允许访问私有
            method.setAccessible(true);

            method.invoke(student,"我传了一个参数");

        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        reflectPrivateMethod();
    }
}
